﻿using System;
using System.Collections.Generic;

namespace PoolKit
{
    internal class GenericPoolDisposableObserverFriendly<TResource> : GenericPool<TResource>
        where TResource : class
    {
        private readonly IEnumerable<IDisposable> _observers;

        internal GenericPoolDisposableObserverFriendly(
            IResourceCollection<TResource> resourceCollection,
            ReusePreparationStrategy<TResource> reusePreparationStrategy,
            RepairStrategy<TResource> repairStrategy,
            ICommandProcessor commandProcessor,
            IEnumerable<IDisposable> observers) : base(resourceCollection, reusePreparationStrategy, repairStrategy, commandProcessor)
        {
            if (observers == null) throw new ArgumentNullException("observers");

            _observers = observers;
        }

        protected override void Dispose(bool calledFromFinalizer)
        {
            foreach (IDisposable disposableObservers in _observers)
            {
                disposableObservers.Dispose();
            }

            base.Dispose(calledFromFinalizer);
        }
    }
}